package edu.gyc.hiseckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gyc.hiseckill.dao.ItemDao;
import edu.gyc.hiseckill.dao.ItemStockDao;
import edu.gyc.hiseckill.error.BusinessErrorEnum;
import edu.gyc.hiseckill.error.BusinessException;
import edu.gyc.hiseckill.model.Item;
import edu.gyc.hiseckill.model.ItemStock;
import edu.gyc.hiseckill.model.Promo;
import edu.gyc.hiseckill.service.ItemService;
import edu.gyc.hiseckill.service.ItemStockService;
import edu.gyc.hiseckill.service.PromoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-01-23
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemDao, Item> implements ItemService {

    @Autowired
    private ItemStockService itemStockService;

    @Autowired
    private ItemStockDao itemStockDao;

    @Autowired
    private PromoService promoService;


    @Override
    //商品入库，查询商品库存，返回有库存数据的商品对象
    public Item createItem(Item item) throws BusinessException {
        if (!save(item)) {
            throw new BusinessException(BusinessErrorEnum.ITEM_SAVE_ERROR);
        }
        ItemStock itemStocktmp = new ItemStock();
        itemStocktmp.setItemId(item.getId());
        itemStocktmp.setStock(item.getStock());
        if (!itemStockService.save(itemStocktmp)) {
            throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "商品库存量入库有误");
        }

        return item;
    }

    @Override
    public Item getItem(Integer id) throws BusinessException {
        Item item = getById(id);
        if (item == null) {
            throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR,"商品编号有误查询不出");
        }
        ItemStock itemStock= itemStockService.lambdaQuery().eq(ItemStock::getItemId, item.getId()).one();
        if(itemStock!=null){
            item.setStock(itemStock.getStock());
        }else {
            item.setStock(0);
        }
        //获取活动商品信息
        Promo promo= promoService.getPromoByItemId(item.getId());
        if (promo != null && promo.getStatus() != 3) {
            item.setPromo(promo);
        }
        return item;
    }

    @Override
    public List<Item> listItem() {
        List<Item> items = this.list();
        for (Item item : items) {
            ItemStock itemStock= itemStockService.lambdaQuery().eq(ItemStock::getItemId, item.getId()).one();
            if(itemStock!=null){
                item.setStock(itemStock.getStock());
            }else {
                item.setStock(0);
            }

        }
        return items;
    }

    @Override

    public void decreaseStock(Integer itemId, Integer amount) throws BusinessException {
        // //用上了乐观锁 sql语句版本
        ItemStock stockItem=itemStockService.lambdaQuery().eq(ItemStock::getItemId,itemId).one();
        int oldversion=stockItem.getVersion();
        int affectRow = itemStockDao.decreaseStock(itemId, amount,oldversion);
        if (affectRow ==0) {
            throw new BusinessException(BusinessErrorEnum.STOCK_ENOUGH_ERROR);

        }



       //用上了乐观锁
        /*
       ItemStock stockItem=itemStockService.lambdaQuery().eq(ItemStock::getItemId,itemId).one();

       int stock=stockItem.getStock()-amount;
        if (stock < 0) {
            throw new BusinessException(BusinessErrorEnum.STOCK_ENOUGH_ERROR);
        }

       int version=stockItem.getVersion();



       ItemStock itemStock=new ItemStock();
       itemStock.setId(stockItem.getId());
       itemStock.setItemId(stockItem.getItemId());
       itemStock.setStock(stock);
       itemStock.setVersion(version);

        int n = itemStockDao.updateById(itemStock);
        if (n ==0) {
            throw new BusinessException(BusinessErrorEnum.STOCK_ENOUGH_ERROR);

        }
     */
    }

    @Override

    public void increaseSales(Integer itemId, Integer amount) throws BusinessException {
       /*
        int oldSales = lambdaQuery().eq(Item::getId, itemId).one().getSales();
       boolean r= this.lambdaUpdate().eq(Item::getId, itemId).set(Item::getSales, oldSales + amount).update();
       if(!r){
           throw new BusinessException(BusinessErrorEnum.STOCK_ENOUGH_ERROR);
       }

        */
        //乐观锁更新销售量
        Item item = getById(itemId);
        int oldSales=item.getSales();
        int version=item.getVersion();

        Item updateItem=new Item();
        updateItem.setId(item.getId());
        updateItem.setSales(oldSales+amount);
        updateItem.setVersion(version);
        int n = this.getBaseMapper().updateById(updateItem);

        if (n ==0) {
            throw new BusinessException(BusinessErrorEnum.STOCK_ENOUGH_ERROR,"商品销售量更新错误");

        }
    }
}
